<template>
  <el-dialog
    :close-on-click-modal="false"
    :title="plan_name == 'plan' ? '计划批量修改' : '采购批量修改'"
    v-model="visible"
    align-center
    width="600px"
    destroy-on-close
  >
    <el-form
      ref="formRef"
      :model="form"
      label-position="top"
      :label-width="140"
    >
      <div class="grid grid-cols-2 gap-x-3">
        <template v-if="plan_name == 'plan'">
          <el-form-item label="Asin">
            <el-input
              v-model="form.asin"
              placeholder="请输入"
              :maxlength="200"
            />
          </el-form-item>
          <el-form-item label="平台">
            <VSelect
              v-model="form.platform"
              :options="options.platform"
              select
              placeholder="平台"
              class="w-full"
              @change="form.plan_type = form.platform === 7 ? 2 : 1"
            />
          </el-form-item>
          <el-form-item label="国家">
            <VSelect
              v-model="form.country"
              :options="options.country"
              class="w-full"
              placeholder="请选择"
              @change="form.destination_warehouse = ''"
            />
          </el-form-item>
          <el-form-item label="类型">
            <VSelect
              v-model="form.plan_type"
              placeholder="请选择"
              :options="options.plan_type"
              class="w-full"
            />
          </el-form-item>

          <el-form-item label="销售">
            <VCascader
              v-model="form.sale_id"
              class="w-full"
              placeholder="请选择"
              @change="(val) => saleChange(form, val)"
            />
          </el-form-item>
          <el-form-item label="部门">
            <el-input
              :value="form.department_name"
              placeholder="请选择"
              disabled
              class="w-full"
            />
          </el-form-item>
          <el-form-item label="数量">
            <el-input-number
              v-model="form.plan_num"
              controls-position="right"
              placeholder="请输入"
              :min="0"
              :step="1"
              :precision="0"
              class="w-full"
            />
          </el-form-item>
          <el-form-item label="运输方式">
            <VSelect
              v-model="form.transport_type"
              placeholder="请选择"
              :options="options.transport_type"
              class="w-full"
            />
          </el-form-item>
          <el-form-item label="计划预计下单时间">
            <el-date-picker
              v-model="form.expect_order_time"
              placeholder="日期选择"
              type="date"
              value-format="YYYY-MM-DD"
              class="w-full"
            />
          </el-form-item>
          <el-form-item label="预计交期">
            <el-date-picker
              v-model="form.pre_delivery_time"
              type="date"
              value-format="YYYY-MM-DD"
              class="w-full"
            />
          </el-form-item>
          <el-form-item label="目的仓">
            <VSelect
              v-model="form.destination_warehouse"
              placeholder="请选择"
              :options="
                form.country === 'US'
                  ? options.destination_warehouse
                  : options.plan_destination_warehouse
              "
              class="w-full"
            />
          </el-form-item>
          <el-form-item label="备注" class="col-span-full">
            <el-input
              v-model="form.remark"
              placeholder="请输入"
              type="textarea"
              :autosize="{ minRows: 2, maxRows: 5 }"
            />
          </el-form-item>
        </template>
        <template v-else>
          <el-form-item label="采购人员" class="col-span-full">
            <VCascader
              v-model="form.purchase_user_id"
              placeholder="请选择"
              class="w-full"
            />
          </el-form-item>
          <el-form-item label="预计到货时间" class="col-span-full">
            <el-date-picker
              v-model="form.pre_factory_time"
              type="date"
              value-format="YYYY-MM-DD"
              class="w-full"
            />
          </el-form-item>
        </template>
      </div>
    </el-form>
    <template #footer>
      <el-button @click="handleClose(false)">取 消</el-button>
      <el-button type="primary" :loading="loading" @click="save(formRef)"
        >确 定</el-button
      >
    </template>
  </el-dialog>
</template>

<script setup>
import { reactive } from "vue";
import api from "../api";
import { ElMessage } from "element-plus";
import { getDepartment } from "@/api/public";
import { resMessage } from "@/utils/res";
import { isObjectEmptyOrAllFieldsEmpty, removeEmptyFields } from "../data";
const props = defineProps({
  options: { type: Object, default: () => ({}) },
});
const emit = defineEmits(["close", "success"]);

const visible = ref(false);
const loading = ref(false);

// 表单处理
const formRef = ref();
const form = ref({});

const saleChange = (form, { value }) => {
  getDepartment({ select_user_id: value }).then((res) => {
    form.department_id = res.data.department_id;
    form.department_name = res.data.department_name;
  });
};
const plan_name = ref(null);
const pp_ids_data = ref([]);
// 打开弹窗
const open = async (val) => {
  const { pp_ids, name } = val;
  plan_name.value = name;
  pp_ids_data.value = pp_ids || [];
  visible.value = true;
};
const handleClose = (refresh) => {
  visible.value = false;
  form.value = {};
  pp_ids_data.value = [];
  formRef?.value?.resetFields();
  emit("close");
  refresh && emit("success");
};
// 提交
const save = async (formRef) => {
  if (!(await formRef.validate())) return;
  if (isObjectEmptyOrAllFieldsEmpty(form.value))
    return ElMessage.warning("没有修改数据!");
  let filteredForm = removeEmptyFields(form.value);
  filteredForm.pp_ids = pp_ids_data.value;
  loading.value = true;
  try {
    const res = await api.batch_plan(filteredForm);
    resMessage({
      res,
      success: () => {
        ElMessage.success("保存成功");
        handleClose(true);
      },
    });
  } finally {
    loading.value = false;
  }
};

defineExpose({ open });
</script>
